<template>
  <el-dialog title="编辑管理员" :visible.sync="dialogVisible" width="700px" :before-close="handleCancel">
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="80px">
      <el-form-item label="账号" prop="account">
        <el-input class="create_admin__input" v-model.number="ruleForm.account"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input class="create_admin__input" type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPassword">
        <el-input class="create_admin__input" type="password" v-model="ruleForm.checkPassword" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleCancel">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { updateAgency } from '@/api/organize'

export default {
  name: 'CreateAdmin',
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (value.length < 6) {
          callback(new Error('密码不能小于6位'))
        }else{
          if (this.ruleForm.checkPassword !== '') {
            this.$refs.ruleForm.validateField('checkPassword')
          }
          callback()
        }
      }
    }
    const validateCheckPass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      ruleForm: {
        account: '',
        password: '',
        checkPassword: ''
      },
      validatePass,
      validateCheckPass,
      rules: {
        password: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        checkPassword: [
          { required: true, validator: validateCheckPass, trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ]
      }
    }
  },

  props: {
    dialog: {
      type: Boolean,
      default: false
    },
    details: {
      type: Object,
      default: {}
    }
  },

  watch: {
    dialog(boolean) {
      this.dialogVisible = boolean
    },
    details(params) {
      const { account, id } = params
      this.ruleForm = {
        ...this.ruleForm,
        account,
        userId: id
      }
    }
  },
  methods: {
    // 表单验证
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.handleUpdateAgency()
        } else {
          return false
        }
      })
    },

    // 编辑区域
    handleUpdateAgency() {
      const { account, password, userId } = this.ruleForm
      updateAgency({ account, password, userId })
        .then(res => {
          this.dialogVisible = false
          this.$message({
            message: '编辑管理员成功！',
            type: 'success'
          })
          this.resetForm('ruleForm')
          this.$emit('edit')
        })
    },

    // 表单重置
    resetForm(name) {
      this.$refs[name].resetFields()
    },

    handleCancel() {
      // this.resetForm('ruleForm')
      this.$emit('cancel')
    }
  }
}
</script>

<style lang="scss" scoped>
.create_admin__input {
  width: 470px;
}
</style>
